using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace MPSizectorDotNet
{
	public class MPSizectorSelectDialog : Form
	{
		public MPSizector Device;

		private int SelectedDeviceIndex = 0;

		private IContainer components = null;

		private DataGridView DeviceListDgv;

		private Button OpenBtn;

		private Button CancleBtn;

		private Timer RefreshTimer;

		private DataGridViewTextBoxColumn Index;

		private DataGridViewTextBoxColumn Model;

		private DataGridViewTextBoxColumn Serial_Number;

		private DataGridViewTextBoxColumn Resolution;

		private DataGridViewTextBoxColumn Interface;

		private DataGridViewTextBoxColumn Column1;

		private DataGridViewTextBoxColumn Column2;

		public MPSizectorSelectDialog()
		{
			this.InitializeComponent();
		}

		private void MPSizectorSelectDialog_Load(object sender, EventArgs e)
		{
			this.Device = new MPSizector();
			this.UpdateList();
			this.RefreshTimer.Enabled = true;
		}

		private void OpenBtn_Click(object sender, EventArgs e)
		{
			float minimumSDKVersion = this.Device.GetDeviceInfo(this.SelectedDeviceIndex).MinimumSDKVersion;
			float sizectorDotNetVersion = MPSizector.GetSizectorDotNetVersion();
			if (minimumSDKVersion > sizectorDotNetVersion)
			{
				MessageBox.Show("当前SDK版本低于设备所要求的最低版本号. 程序将继续运行, 但可能出现不可预知的错误. \n\n The SDK version required by device is higher than current SDK version. Error could occur through operation.", "MPSizectorDotNet Select Dialog", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
			MPDeviceInfo deviceInfo = this.Device.GetDeviceInfo(this.SelectedDeviceIndex);
			if (this.Device.Open(deviceInfo))
			{
				base.DialogResult = DialogResult.OK;
			}
			else
			{
				MessageBox.Show("Device Open Failed!");
				base.DialogResult = DialogResult.Abort;
			}
			this.RefreshTimer.Enabled = false;
			base.Close();
		}

		private void CancleBtn_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			this.RefreshTimer.Enabled = false;
			base.Close();
		}

		private void RefreshTimer_Tick(object sender, EventArgs e)
		{
			this.UpdateList();
			this.RefreshTimer.Interval = 3000;
		}

		private void UpdateList()
		{
			int num = 0;
			int num2 = -1;
			if (this.DeviceListDgv.SelectedRows.Count == 1)
			{
				num2 = int.Parse((string)this.DeviceListDgv.SelectedRows[0].Cells[0].Value);
			}
			this.DeviceListDgv.Rows.Clear();
			this.Device.UpdateDeviceList();
			num = this.Device.GetDeviceCount();
			int i;
			for (i = 0; i < num; i++)
			{
				MPDeviceInfo deviceInfo = this.Device.GetDeviceInfo(i);
				this.DeviceListDgv.Rows.Add(i.ToString(), deviceInfo.DeviceName, deviceInfo.DeviceSerialNumber, deviceInfo.SensorWidth.ToString() + "x" + deviceInfo.SensorHeight.ToString(), deviceInfo.IsUSB30 ? "USB3.0" : "USB2.0", deviceInfo.MinimumSDKVersion.ToString(), deviceInfo.FWVersion.ToString());
			}
			this.DeviceListDgv.ClearSelection();
			if (i > 0)
			{
				if (num2 != -1 && num2 < num)
				{
					this.DeviceListDgv.Rows[num2].Selected = true;
					this.SelectedDeviceIndex = int.Parse((string)this.DeviceListDgv.Rows[num2].Cells[0].Value);
				}
				else
				{
					this.DeviceListDgv.Rows[0].Selected = true;
					this.SelectedDeviceIndex = int.Parse((string)this.DeviceListDgv.Rows[0].Cells[0].Value);
				}
				this.OpenBtn.Enabled = true;
			}
			else
			{
				this.OpenBtn.Enabled = false;
			}
		}

		private void DeviceListDgv_SelectionChanged(object sender, EventArgs e)
		{
			if (this.DeviceListDgv.SelectedRows.Count > 0)
			{
				this.SelectedDeviceIndex = int.Parse((string)this.DeviceListDgv.SelectedRows[0].Cells[0].Value);
			}
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.components = new Container();
			this.DeviceListDgv = new DataGridView();
			this.OpenBtn = new Button();
			this.CancleBtn = new Button();
			this.RefreshTimer = new Timer(this.components);
			this.Index = new DataGridViewTextBoxColumn();
			this.Model = new DataGridViewTextBoxColumn();
			this.Serial_Number = new DataGridViewTextBoxColumn();
			this.Resolution = new DataGridViewTextBoxColumn();
			this.Interface = new DataGridViewTextBoxColumn();
			this.Column1 = new DataGridViewTextBoxColumn();
			this.Column2 = new DataGridViewTextBoxColumn();
			((ISupportInitialize)this.DeviceListDgv).BeginInit();
			base.SuspendLayout();
			this.DeviceListDgv.AllowUserToAddRows = false;
			this.DeviceListDgv.AllowUserToDeleteRows = false;
			this.DeviceListDgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.DeviceListDgv.Columns.AddRange(this.Index, this.Model, this.Serial_Number, this.Resolution, this.Interface, this.Column1, this.Column2);
			this.DeviceListDgv.EditMode = DataGridViewEditMode.EditProgrammatically;
			this.DeviceListDgv.ImeMode = ImeMode.On;
			this.DeviceListDgv.Location = new Point(12, 12);
			this.DeviceListDgv.Name = "DeviceListDgv";
			this.DeviceListDgv.RowTemplate.Height = 23;
			this.DeviceListDgv.ScrollBars = ScrollBars.Vertical;
			this.DeviceListDgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			this.DeviceListDgv.Size = new Size(714, 250);
			this.DeviceListDgv.TabIndex = 0;
			this.DeviceListDgv.SelectionChanged += this.DeviceListDgv_SelectionChanged;
			this.OpenBtn.Location = new Point(605, 278);
			this.OpenBtn.Name = "OpenBtn";
			this.OpenBtn.Size = new Size(121, 34);
			this.OpenBtn.TabIndex = 2;
			this.OpenBtn.Text = "Open";
			this.OpenBtn.UseVisualStyleBackColor = true;
			this.OpenBtn.Click += this.OpenBtn_Click;
			this.CancleBtn.Location = new Point(12, 278);
			this.CancleBtn.Name = "CancleBtn";
			this.CancleBtn.Size = new Size(121, 34);
			this.CancleBtn.TabIndex = 3;
			this.CancleBtn.Text = "Cancle";
			this.CancleBtn.UseVisualStyleBackColor = true;
			this.CancleBtn.Click += this.CancleBtn_Click;
			this.RefreshTimer.Tick += this.RefreshTimer_Tick;
			this.Index.HeaderText = "Index";
			this.Index.Name = "Index";
			this.Index.Width = 50;
			this.Model.HeaderText = "Model";
			this.Model.Name = "Model";
			this.Serial_Number.HeaderText = "Serial Number";
			this.Serial_Number.Name = "Serial_Number";
			this.Serial_Number.Width = 150;
			this.Resolution.HeaderText = "Resolution";
			this.Resolution.Name = "Resolution";
			this.Interface.HeaderText = "Interface";
			this.Interface.Name = "Interface";
			this.Interface.Width = 70;
			this.Column1.HeaderText = "Minimum SDK Version";
			this.Column1.Name = "Column1";
			this.Column2.HeaderText = "Device FW Version";
			this.Column2.Name = "Column2";
			base.AutoScaleDimensions = new SizeF(6f, 12f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(738, 333);
			base.Controls.Add(this.CancleBtn);
			base.Controls.Add(this.OpenBtn);
			base.Controls.Add(this.DeviceListDgv);
			base.Name = "MPSizectorSelectDialog";
			base.ShowIcon = false;
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = "Sizector Select Dialog";
			base.Load += this.MPSizectorSelectDialog_Load;
			((ISupportInitialize)this.DeviceListDgv).EndInit();
			base.ResumeLayout(false);
		}
	}
}
